﻿Imports System
Imports System.IO
Imports System.Data.SqlServerCe
Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.Common

Public Class frmWeldingSeq

    Sub initScreen()
        Me.tabZoneSeq.SelectedIndex = 0
        Me.tabZoneSeq.Refresh()
    End Sub

    Private Sub frmWeldingSeq_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadZone(cboWeldingSeqZone, cboWeldingSeqZoneFilter)
    End Sub

    Private Sub MenuItemLogOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemLogOut.Click
        _FROM_SCREEN_ID = _SCREEN_ID
        _SCREEN_ID = SCREEN_PROJECT

        frmProject.Show()
        frmProject.initScreen()
        Me.Hide()
    End Sub

    Private Sub MenuItemCoupon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemMenu.Click
        frmMenu.Show()
        frmMenu.initScreen()
        Me.Hide()
    End Sub

    Private Sub btnWeldingSeqSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWeldingSeqSave.Click
        Dim returnExecute As Boolean = False
        If (cboWeldingSeqZone.Text = "") Then
            MsgBox("Zone cannot be blank.", MsgBoxStyle.Information, "EQAM")
            Return
        End If
        If (txtWeldingSeqBarcode.Text = "") Then
            MsgBox("Barcode cannot be blank.", MsgBoxStyle.Information, "EQAM")
            Return
        End If
        If (txtWeldingSeqNo.Text = "") Then
            MsgBox("Seq No. cannot be blank", MsgBoxStyle.Information, "EQAM")
            Return
        End If

        returnExecute = saveWeldSeq(_PROJECT_ID, cboWeldingSeqZone.Text, txtWeldingSeqBarcode.Text, txtWeldingSeqNo.Text)
        If returnExecute = True Then
            MsgBox("Save Successfully.", MsgBoxStyle.Information, "EQAM")
            txtWeldingSeqNo.Text = getZoneSeq(cboWeldingSeqZone.Text, _PROJECT_ID)
            txtWeldingSeqBarcode.Text = ""
            txtWeldingSeqBarcode.Focus()
        Else
            'MessageBox.Show("Error!")
            Return
        End If
    End Sub

    Private Sub cboWeldingSeqZone_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboWeldingSeqZone.SelectedIndexChanged
        If (cboWeldingSeqZone.Text IsNot "") Then
            txtWeldingSeqNo.Text = getZoneSeq(cboWeldingSeqZone.Text, _PROJECT_ID)
        Else
            txtWeldingSeqNo.Text = 1
        End If
    End Sub

    Private Sub tabZoneSeq_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tabZoneSeq.SelectedIndexChanged
        If (Me.tabZoneSeq.SelectedIndex = 1) Then
            refreshLvWeldingSeq()
            Me.tabZoneSeq.Refresh()
        End If
    End Sub

    Private Sub cboWeldingSeqZoneFilter_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboWeldingSeqZoneFilter.SelectedIndexChanged
        refreshLvWeldingSeq()
    End Sub

    Sub refreshLvWeldingSeq()
        Dim lstView As New ListViewItem
        Dim dtResult As New DataTable

        Me.lvWeldingSeq.Items.Clear()
        If (cboWeldingSeqZoneFilter.Text <> "") Then
            dtResult = findWeldSeq(_PROJECT_ID, cboWeldingSeqZoneFilter.Text)

            If dtResult.Rows.Count > 0 Then
                For Each dataRow As DataRow In dtResult.Rows
                    lstView = Me.lvWeldingSeq.Items.Add(New ListViewItem(dataRow.Item("SEQ").ToString))
                    lstView.SubItems.Add(dataRow.Item("BARCODE").ToString)
                    lstView.SubItems.Add(dataRow.Item("ZONE").ToString)

                    lstView.Tag = dataRow.Item("SEQ_ID").ToString
                Next                
            End If
        End If
    End Sub

    Private Sub btnWeldingSeqDeleteAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWeldingSeqDeleteAll.Click
        deleteWeldSeq(_PROJECT_ID, cboWeldingSeqZoneFilter.Text, Nothing)
        refreshLvWeldingSeq()
    End Sub

    Private Sub btnWeldingSeqDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWeldingSeqDelete.Click
        Dim list As New ArrayList

        ' save coupon system
        Dim lstItem As ListViewItem
        For Each lstItem In lvWeldingSeq.Items
            If (lstItem.Checked) Then
                Dim seqId As String = lstItem.Tag
                list.Add(seqId)
            End If
        Next

        deleteWeldSeq(_PROJECT_ID, cboWeldingSeqZoneFilter.Text, list)

        refreshLvWeldingSeq()
    End Sub
End Class